﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nanoCMS.Core.Concrete;

namespace nanoCMS.Core.Providers
{
    public class MetaTagProvider
    {
        private readonly Entities entities;
        private readonly CacheProvider cacheProvider;

        public MetaTagProvider(Entities entities)
        {
            this.entities = entities;
            cacheProvider = CacheProvider.Default;
        }


        private static string CacheKey(int tenantID, int langID)
        {
            return String.Concat("TenantMetaTag", "_", tenantID.ToString(), "_", langID);
        }

        #region Add

        /// <summary>
        /// Adds new tenantMetatag
        /// </summary>
        /// <param name="title"></param>
        /// <param name="keywords"></param>
        /// <param name="description"></param>
        /// <param name="tenantID"></param>
        /// <param name="langID"></param>
        /// <returns></returns>
        public TenantMetatag Add(string title, string keywords, string description, int tenantID, int langID)
        {
            //if (entities.TenantMetatags.SingleOrDefault(x => x.TenantID == tenantID && x.LangID == langID) == null)
              //  throw new NotImplementedException();

            TenantMetatag tenantMetatag = new TenantMetatag
                                          {
                                              Title = title,
                                              Keywords = keywords,
                                              Description = description,
                                              TenantID = tenantID,
                                              LangID = langID
                                          };
            entities.TenantMetatags.AddObject(tenantMetatag);
            entities.SaveChanges();
            return tenantMetatag;
        }

        /// <summary>
        /// Used if trying to get non existant entity
        /// </summary>
        /// <returns></returns>
        private TenantMetatag AddEmpty(int tenantID, int langID)
        {
            return Add(String.Empty, String.Empty, String.Empty, tenantID, langID);
        }
        #endregion

        #region Get
        public TenantMetatag Get(int tenantID, int langID = 1)
        {
            string cacheKey = CacheKey(tenantID, langID);
            if (cacheProvider.Contains(cacheKey))
                return cacheProvider.Get(cacheKey) as TenantMetatag;
            
            var dbValue = entities.TenantMetatags.SingleOrDefault(x => x.TenantID == tenantID && x.LangID == langID) 
                ?? AddEmpty(tenantID, langID);

            cacheProvider.Add(cacheKey, dbValue);
            return dbValue;
        }
        #endregion
        

        #region Edit
        public void Edit(string title, string keywords, string description, int tenantID, int langID)
        {
            var dbValue = entities.TenantMetatags.SingleOrDefault(x => x.TenantID == tenantID && x.LangID == langID)
                ?? AddEmpty(tenantID, langID);

            dbValue.Title = title;
            dbValue.Keywords = keywords;
            dbValue.Description = description;
            entities.SaveChanges();

            cacheProvider.Remove(CacheKey(tenantID, langID));
        }
        #endregion
    }
}


